<?php
/**
 * 用户信息
 * @copyright (c) Seer All Rights Reserved
 */

class User_Master_Model {

	private $db;

	function __construct() {
		$this->db = Database::getInstance();
	}

	/**
	 * 添加用户master数据
	 *
	 */
	function addUserMaster() {
		$time=date("Y-m-d H:i:s");
		$sql="insert into ".DB_PREFIX."user_master (point,hot,create_time,modify_time) values(100,0,'$time','$time')";
		$this->db->query($sql);
		return mysql_insert_id();
	}

	/**
	 * 编辑用户master数据
	 *
	 */
	function updateUserMaster($userMasterData) {
		$time=date("Y-m-d H:i:s");
		$familyId = $userMasterData["family_id"];
		foreach ($userMasterData as $key => $data) {
			$Item[] = "$key='$data'";
		}
		$upStr = implode(',', $Item);

		$this->db->query("update ".DB_PREFIX."user_master set $upStr,modify_time=$time where family_id=$familyId");
	}

	/**
	 * 编辑用户master数据
	 *
	 */
	function updateUserMasterHot($userMasterData) {
		$time=date("Y-m-d H:i:s");
		$familyId = $userMasterData["family_id"];
		$this->db->query("update ".DB_PREFIX."user_master set hot=hot+1,modify_time=$time where family_id=$familyId");
	}

	/**
	 * 编辑用户master数据
	 *
	 */
	function updateUserMasterPoint($userMasterData) {
		$time=date("Y-m-d H:i:s");
		$familyId = $userMasterData["family_id"];
		$point = $userMasterData["point"];
		$this->db->query("update ".DB_PREFIX."user_master set point=point+$point,modify_time=$time where family_id=$familyId");
	}

	/**
	 * 得到用户信息
	 *
	 * @param string $open_id
	 */
	function getUserMaster($familyId) {
		$sql = "SELECT * FROM " . DB_PREFIX . "user_master WHERE family_id = '$familyId'";
		$res = $this->db->once_fetch_array($sql);
		$user_master = array();
		if($res){
			$user_master = $res;
		}
		return $user_master;
	}

	/**
	 * 得到用户热度排名
	 *
	 * @param string $familyid
	 */
	function getHotIndex($myHot) {
		$sql = "SELECT count(*) as total FROM " . DB_PREFIX . "user_master WHERE hot < '$myHot'";
		$res = $this->db->once_fetch_array($sql);
		return $res['total'];;
	}
}
